/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet TABFOLDER
 VERSION:0.85
 DATE:15/06/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
Name			Name of the HTML element
top				Top position (in pixel)
left			Left position (in pixel)
width			Width (in pixel or %)
height			Height (in pixel or %)
parent			Reference of an HTML or a javascript element parent of the tab html element
widthTabName	Tab Size
autoRedimTab	True enables the automatic redimensioning of tabs' titles
isClosable		if true, the tabs can be close
draggableItem   if true the tab item can be drag and drop
*************************************************************************************************/

// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.TabFolder=function(objPar) {
	
	//base constructor
    this.base = rialto.widget.AbstractContainer;
    objPar.type = "tabFolder";
    this.base(objPar);
    
	//Default value
    this.widthTabName = 100;
    this.autoResizeContenu = false;
    this.autoResizeParent = false;    
    this.autoRedimTab = false;    
    this.isClosable = true;
    this.draggableItem = false;
    
    //set the value with the properties of the objPar object
    if (rialto.lang.isBoolean(objPar.autoResizeContenu)){
        this.autoResizeContenu = objPar.autoResizeContenu;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizeParent)){        
        this.autoResizeParent = objPar.autoResizeParent;        
        this.autoResizableH=this.autoResizeParent;
    }
    if (rialto.lang.isBoolean(objPar.autoRedimTab)){
        this.autoRedimTabt = objPar.autoRedimTab;        
    }
    if (rialto.lang.isBoolean(objPar.isClosable)){
        this.isClosable = objPar.isClosable;
    }
    if (rialto.lang.isBoolean(objPar.draggableItem)){
        this.draggableItem = objPar.draggableItem;
    }  
    if (rialto.lang.isNumber(objPar.widthTabName)){
        this.widthTabName = objPar.widthTabName;
    }    
	var oThis=this;

	// *******************************************
	// GENERATION OF DOM ELEMENTS
	// *******************************************
    
    this.divExt.style.top=this.top;
    this.divExt.style.left=this.left;
    this.divExt.style.height=this.height;
    this.divExt.style.width=this.width;                
    this.divExt.style.position=this.position;
    this.divExt.style.backgroundColor="#F3F3F3";
    this.divExt.style.overflow='hidden';
    var oThis=this;
    // DIV that will contain the tab
    this.divOnglet = document.createElement("DIV");
    this.divOnglet.id = "DIV_ONGLETS";        
    this.divOnglet.style.height='21px';
    this.divOnglet.style.width='100%';            
    this.divExt.appendChild(this.divOnglet);
    // DIV that will containt the content of the tab
    this.divContenuGlobal = document.createElement("DIV");
    this.divContenuGlobal.id = "DIV_CONTENUGLOBAL";            
    this.divContenuGlobal.style.border = "1px solid #6D94AB";
    this.divExt.appendChild(this.divContenuGlobal);
	
	//Array of tab item          
    this.arrTabItem=new Array();        
    
    //Object to save the visible part of the tab item
    this.fenVisible=new Object;        
    this.fenVisible.indDeb=0;
    this.fenVisible.indFin=0;
    this.fenVisible.tailleFen=0;
    
  
    this.swapInProgress=false; 
    // Add in the parent      
    if (objPar.parent) {this.placeIn(objPar.parent);}; 

	//release the object
	objPar=null;
	
	
    this.lefTitre=0;
    
    //Div that will contain IMAGES (close,next...)

    this.divImg=document.createElement("DIV");
    this.divImg.className="divImg";
    this.divImg.style.display='none';
    this.divOnglet.appendChild(this.divImg);
        
        
    if(!this.autoRedimTab){        
        //create the menu
        var obj={name:"MENU-ONGLET",className:"ong-opslist"};
        this.liste=new rialto.widget.simpleMenu(obj);                        
          
          
        ob={position:'relative',boolFloatRight:true}
    
        //close button        
        if(this.isClosable){
	        this.btnClose=new rialto.widget.Image("crossCloseOff",-2,3,this.divImg,"Fermer l'onglet courant","crossCloseOn",ob);	        
	        this.btnClose.onclick=function(){        	
	            oThis.removeTabItem(oThis.indActiveTab);            
	        }  
	    }
             
        //open list tab
        ob={position:'relative',boolFloatRight:true,imageDisabled:"leftArrowGroundlessOff"}        
        
        this.MC = new rialto.widget.Image("tabListOff",-8,3,this.divImg,"Choose another tab","tabListOn",ob);        
        this.MC.setVisible(false);        
        this.MC.onclick=function (e) {           
            var ev = e || window.event;
            oThis.displayMenu(ev);
        };          
        //next 
        ob={position:'relative',boolFloatRight:true,imageDisabled:"rightArrowGroundlessOff"}
        
        this.next = new rialto.widget.Image("rightArrowOff",-11,3,this.divImg,"next tab","rightArrowOn",ob);        
        this.next.setVisible(false);        
        this.next.onclick=function () {           
            if(!oThis.swapInProgress) oThis.moveOneTab(1);
        };         
		//previous
        ob={position:'relative',boolFloatRight:true,imageDisabled:"leftArrowGroundlessOff"}        
        this.previous = new rialto.widget.Image("leftArrowOff",-14,3,this.divImg,"previous tab","leftArrowOn",ob);        
        this.previous.setVisible(false);        
        this.previous.onclick=function () {           
            if(!oThis.swapInProgress) oThis.moveOneTab(-1);
        };                        
    }         
}


rialto.widget.TabFolder.prototype = new rialto.widget.AbstractContainer;




rialto.widget.TabFolder.prototype.getHtmlCont=function(){
//============================
//function return the html element container of the frame
    return this.divContenuGlobal;
}


rialto.widget.TabFolder.prototype.adaptToContext=function(){
//========================    
		//INITIAL HEIGHT AND WIDTH
	    if (this.autoResizableH){		
	        this.divExt.style.height=this.getNewParentHeight()-this.divExt.offsetTop;
	    }	    
	    if (this.autoResizableW){
	        this.divExt.style.width=this.getNewParentWidth()-this.divExt.offsetLeft;
	    }
	    else{
	        this.divExt.style.width= this.width;
	    }
		this.divContenuGlobal.style.height=this.divExt.offsetHeight-this.divContenuGlobal.offsetTop-10+'px';
}


//UPDATE SIZE

rialto.widget.TabFolder.prototype.updateSize= function(){       	
    this.divContenuGlobal.style.overflow='hidden';
    if (this.autoResizableH){            
        this.updateHeight();        
    }   
    if(this.autoResizableW){        
        this.updateWidth();
    }    
    this.divContenuGlobal.style.overflow='auto';
}

rialto.widget.TabFolder.prototype.updateWidth=function(){  	
   if(this.open == true){
        if (this.autoResizableW){
	        //get new width of the parent
		    var tailleCalc=parseInt(this.getNewParentWidth());
		    this.divExt.style.width=tailleCalc-this.divExt.offsetLeft;		    
		    //call childs resize
		    this.resizeChilds(true,false);       
		}
	}
}

rialto.widget.TabFolder.prototype.updateHeight=function(){        
    if(this.open == true){
        if (this.autoResizableH){
	        //get new height of the parent
		    var tailleCalc=parseInt(this.getNewParentHeight());
		    this.divExt.style.height=tailleCalc-this.divExt.offsetTop;
		    this.divContenuGlobal.style.height=tailleCalc-this.divContenuGlobal.offsetTop-10+'px';
			//call childs resize
		    this.resizeChilds(false,true);       
		    //redim titles
		    this.redimTitreOnglet(); 
		}
	}
    
}


//to move left or right a tab
//parameter: nbreTab +1 ou -1 (way to move)
rialto.widget.TabFolder.prototype.moveOneTab = function (nbreTab) {
//========================                                
    //get the two tab to change
    if(nbreTab>0){
        //move to the right      
        this.ongAaug=this.arrTabItem[this.fenVisible.indFin+1];
        this.ongAdim =this.arrTabItem[this.fenVisible.indDeb];
        indongADim=this.fenVisible.indDeb;        
    }
    else{            
        //move to the left
        this.ongAaug =this.arrTabItem[this.fenVisible.indDeb-1];            
        this.ongAdim=this.arrTabItem[this.fenVisible.indFin];
        indongADim=this.fenVisible.indFin;        
    } 
    //update index of the first visible and the last visible
    this.fenVisible.indDeb+=nbreTab;
    this.fenVisible.indFin+=nbreTab;
    
    //change the size
    this.ongAaug.setWidth(0);
    this.ongAaug.displayTitle();
    
    //if the tab that must be hide is active we select his brother (left or rigth according to the gap)
    if(this.indActiveTab==indongADim){                        
        this.activeTab(indongADim+nbreTab);        
    }    
    //update image state
    this.majImages();        
    this.swapEffect()        
}

rialto.widget.TabFolder.prototype.swapEffect=function (oldDelai){    
//========================
    //to have only one move
    this.swapInProgress=true;
    if (!oldDelai){delai=100;}
    else{delai=oldDelai/10;}
    if (parseInt(this.ongAdim.titreOnglet.style.width)>20){
        this.ongAdim.changeWidth(-20);
        this.ongAaug.changeWidth(+20);
        this.time=window.setTimeout("rialto.session.objects['"+this.id+"'].swapEffect("+delai+");",delai);
    }
    else{
        this.ongAdim.setWidth(this.widthTabName);
        this.ongAdim.setVisible(false);
        this.ongAaug.setWidth(this.widthTabName);
        //end of the move
        this.swapInProgress=false;
    }
}


rialto.widget.TabFolder.prototype.activeTab = function (nOnglet) {
//parameter:nOnglet index of the tab to activate
    if (this.arrTabItem.length < nOnglet){
        alert('pas d\'onglet n? '+nOnglet);
    }
    else {
    	//get the corresponding tab item
	    var onglet=this.arrTabItem[nOnglet]; 
	    if(onglet.enable){
	        //if not visible make it appear
	        if(!this.autoRedimTab){
	            this.setTabVisible(nOnglet);       
	        }                	     
	        //call the activate method of the tab item
	        onglet.activeOnglet();        
	        this.indActiveTab=nOnglet;
	    }
    }    
}

//TODO after the Drag and drop
/*parameter:
tab:tab item that have been drop
place:new index 
sens:true if the tab was move to the right
*/
rialto.widget.TabFolder.prototype.replaceAfterDD = function (tab, place, sens) {
//========================                
    // insert in DOM
    //if it is the last position  we make an append
    if(place==this.arrTabItem.length-1){
        this.divOnglet.appendChild(tab.titreOnglet);
    }
    else{        
        //if we move to the right the insert position is place+1
        if(sens) {
            placeInser=place+1;        
        }
        else{
            placeInser=place;
        }
        //insert the tab title at his insert position
        this.divOnglet.insertBefore(tab.titreOnglet,this.arrTabItem[placeInser].titreOnglet);        
    }	
    //update the tab array
    var oldPlace = rialto.array.indexOf(this.arrTabItem,tab);    
    this.arrTabItem.splice(oldPlace,1);    
    rialto.array.insert(this.arrTabItem,place,tab);    
    //activate the tab item we have just move
    this.activeTab(place);

}
//to add a new tab item
rialto.widget.TabFolder.prototype.addTabItem=function(titre,enable) {                
//========================
    var onglet=new rialto.widget.TabItem(titre,this,enable);                                       
    if(this.isClosable){    	
    	//for the first tab we show the close button        
	    if (this.arrTabItem.length==0){
		    //update image area
		    this.divImg.style.display='block';
	    }	    
    }
                
    //add the tab item in the array
    var ind=this.arrTabItem.push(onglet);                                        
    
    //add the HTML elements of the tabItem
    onglet.placeIn(this.divContenuGlobal);                
    this.divOnglet.appendChild(onglet.titreOnglet);
  	//redim title  
    this.redimTitreOnglet(true);        
           
    var oThis = this;
 
    
    /* ******Drag and Drop behavior of the tab item*******/

    if (this.draggableItem){
/*	    
	    onglet.titreOnglet.DD = DragAndDrop;
	    onglet.titreOnglet.DD (
	           {            
	             objetCibleEvt  : onglet.div
	            ,fantome        : { aspect: 'aPlat'}  
	            , actionMup : false
	            ,orientation    : 'h'            
	            ,modCursor      : {avantDD : true, auClic : true}
	           }
	    );
*/	        
   rialto.widgetBehavior.affect(onglet.titreOnglet	
    							  ,'DragAndDrop'
    							  ,{
 								    oHtmlEvtTarget		    : onglet.div
 								    ,ghost					: {aspect:'rect'}
 								    ,bSelectMark 			: false
 								    ,bMUpAction  			: false
    							    ,isWithLimitsDisplayed  : false
                          			,movingLimits 			: {orientation: 'h'}
                          			}
    							  );
	
	    onglet.titreOnglet.afterDD = function (dTop, dLeft) {
	    //------------------------                
	        //Todo after D&drop
	        //according to the move we will calculate the new position
	        
	        //initialc index
	        var indInit = rialto.array.indexOf(oThis.arrTabItem,onglet);
	        //shift according to the move 
	        var decal = parseInt(dLeft / (oThis.widthTabName));
	        //theoric position
	        var nvPlace = indInit + decal;        
	        //the move can be more then the available size because of the image area
	        if(nvPlace>oThis.fenVisible.indFin){
	            nvPlace=oThis.fenVisible.indFin;
	        }
	        else{
	            //if <0 give 1st position
	            if(nvPlace<0){
	                nvPlace=0;
	            }                    
	        }                     
	        //apply change
	        oThis.replaceAfterDD(onglet,nvPlace,(decal > 0));			
	    };
	
	
	    
	    onglet.titreOnglet.afterClic = function () {
	    //------------------------
	        oThis.activeTab(rialto.array.indexOf(oThis.arrTabItem,onglet));
	    };
    
	}
	else{
		onglet.titreOnglet.onclick= function () {
	    	//------------------------
	        oThis.activeTab(rialto.array.indexOf(oThis.arrTabItem,onglet));
	    };	
	}
    
    this.activeTab(ind-1);
    return onglet;
}


//return true if the given index is in the visible area
rialto.widget.TabFolder.prototype.isVisible=function(nOnglet){        
//========================    
    return (nOnglet>=this.fenVisible.indDeb && nOnglet<=this.fenVisible.indFin)
}
        
//set the tab (representing by is index) visible
rialto.widget.TabFolder.prototype.setTabVisible=function(nOnglet){       
//========================     
   if(!this.isVisible(nOnglet)){
        if(nOnglet<this.fenVisible.indDeb){
            this.fenVisible.indDeb=nOnglet
            this.fenVisible.indFin=this.fenVisible.indDeb+this.fenVisible.tailleFen-1;
        }
        else{
            this.fenVisible.indFin=nOnglet
            this.fenVisible.indDeb=this.fenVisible.indFin-this.fenVisible.tailleFen+1;
        }                
        this.majZoneVisible(); 
    }        
}

//shift the visible area 
rialto.widget.TabFolder.prototype.decalFenVisible = function (nbreTab) {
//========================       
    //new start and end indexes
    //for the moment no limit verification
    this.fenVisible.indDeb+=nbreTab;            
    this.fenVisible.indFin=this.fenVisible.indDeb+this.fenVisible.tailleFen-1;
    this.majZoneVisible();
    
    //activation
    if(!this.isVisible(this.indActiveTab)){
        //if the active tab is not visible activate the first visible
        this.activeTab(this.fenVisible.indDeb);
    }
}
        
//update visible area
rialto.widget.TabFolder.prototype.majZoneVisible=function(nOnglet) {       
//========================       
    //hide all the tab
    for(var i=0;i<this.arrTabItem.length;i++){
        this.arrTabItem[i].setVisible(false);      
    }
    //display only the one visible
    for(var i=this.fenVisible.indDeb;i<=this.fenVisible.indFin;i++){
        var onglet=this.arrTabItem[i];
        //if active tab we display the title and the content
        if (i==this.indActiveTab){
			onglet.setVisible(true);
		}
		else{
			//display only the title
			onglet.displayTitle();
		}
    }   
    //update image area           
    this.majImages();   
}

//this method will allow to remove a tab by passing his index
rialto.widget.TabFolder.prototype.removeTabItem = function(nOnglet) {                       
//===============================       
        //get the tab item
        onglet=this.arrTabItem[nOnglet];                
        onglet.onClose();
        
        //remove HTML elements and from array        
        rialto.array.remove(this.arrTabItem,onglet);          
        onglet.remove();        
       //if it was the active tab we activate the brother
        if(nOnglet==this.indActiveTab){
            //redim  with display of the last tab
            this.redimTitreOnglet(true); 
            //activate the last one
            if (this.arrTabItem.length>0){                                                
                this.activeTab(this.arrTabItem.length-1);
            }
        }
        else{
            //if the tab is before the active one we decrease this.indActiveTab
            if(bOnglet<this.indActiveTab){this.indActiveTab-=1}        
            this.redimTitreOnglet();            
        }
        //hide the image area if we remove the last one
        if(this.isClosable){
	        if (this.arrTabItem.length==0){            
		        this.divImg.style.display='none';
	        }	                
        }
}

//Fonction qui va maj la zone de titre  taille/visible....
//UPDATE TITLE AREA (SIZE,VISIBLE)
rialto.widget.TabFolder.prototype.redimTitreOnglet=function(bDisplayEnd) { 
//========================           
    //width kneed to display all the tab
    tailleTot=(this.arrTabItem.length)*this.widthTabName;
    
    //if auto redim
    if(this.autoRedimTab){        
        if (tailleTot> this.divOnglet.offsetWidth-20){
            taille=Math.floor((this.divOnglet.offsetWidth-20)/this.arrTabItem.length);
            for (i=0;i<this.arrTabItem.length;i++){
                //fix the item title size according to teh available size
                this.arrTabItem[i].setWidth(Math.max(0,taille));                            
            }
        }
    }
    else{                                        
        //compare kneed to available width
        if(tailleTot> this.divOnglet.offsetWidth-60){
            //to much item so we look hom much we can show
            this.fenVisible.tailleFen=parseInt((this.divOnglet.offsetWidth-60)/this.widthTabName);            
        } 
        else{
            //it is ok
            this.fenVisible.tailleFen=this.arrTabItem.length;
        }
        //display from the end 
        if(bDisplayEnd){            
			//end index is the last item			           
            this.fenVisible.indFin=this.arrTabItem.length-1;                
            //caluclate the start index
            this.fenVisible.indDeb=this.fenVisible.indFin-this.fenVisible.tailleFen+1;
            //update
            this.majZoneVisible();
        }
        else{
            //display from the start
            this.fenVisible.indDeb=0;
            this.fenVisible.indFin=this.fenVisible.indDeb+this.fenVisible.tailleFen-1;                    
            //make visible the active tab
            this.setTabVisible(this.indActiveTab);                        
        }                
        //maj des images                
        this.majImages();        
    }    
}

rialto.widget.TabFolder.prototype.release=function(e, shift) {
	if(this.liste){
		this.liste.remove();		
	}
	if(this.btnClose){
        this.btnClose.onclick=null;
    }
    if(this.MC){
        this.MC.onclick=null;
    } 
    if(this.next){
        this.next.onclick=null;
    } 
    if(this.previous){
        this.previous.onclick=null;
    }      
}
//open menu to choose the tab in a list
rialto.widget.TabFolder.prototype.displayMenu=function(e, shift) {
//========================           
    var oThis=this;
    
    var width=0;
   
    //Div representing the visible tabs
    divVisible= document.createElement("DIV");
    divVisible.className="ong-Fenvisible";    
    divVisible.style.height=parseInt(this.fenVisible.tailleFen)*22; //+5;    
    divVisible.style.top=this.fenVisible.indDeb * 23;    
    
    //create cursor for the D&d
    divTirette=document.createElement("DIV");                    
	divTirette.className = "tabListIndex";
    divTirette.style.position="absolute";
    divVisible.appendChild(divTirette);     
    divTirette.style.top=((this.fenVisible.tailleFen*22)/2)-15;    
    //clear the list
    this.liste.clear();    
    this.liste.add(divVisible); 
   
      
    //Definition du onClick
    this.liste.onclick=function(item){
        oThis.activeTab(item.ind);        
    }
    
    //keep the index on over
    this.liste.onOver=function (item){         
        divTirette.ind=item.ind;       
    }
    
    
    for(var i=0;i<this.arrTabItem.length;i++)
    {   
        var objStyle=new Object;       
        width=Math.max(width,getTailleTexte(this.arrTabItem[i].title));                

        var obj={text:this.arrTabItem[i].title
             ,heigth:22
             ,clOver:"ong-hilite"
             ,clOut:"ong-item"                        
        }
        //look if the tab item is visible
        if(this.isVisible(i)){            
            objStyle.fontWeight='bold';            
            if(i==this.indActiveTab){                                        
                objStyle.color='#F7891E';                            
            }            
        }        
		//if not enable
        if(!this.arrTabItem[i].enable){
            objStyle.color='#AAAAAA';            
            objStyle.fontWeight='normal';            
            objStyle.fontStyle='italic';
        }        
                
        var item=this.liste.addItem(obj)        
        item.setStyle(objStyle);
    }     
    
    
    
    tailleBord=0;
    if(!window.event){
        tailleBord=2;
    }
    
    this.liste.setWidth(width+20-tailleBord);
    divVisible.style.width=parseInt(width+20-tailleBord);    
        
    divTirette.style.left=parseInt(divVisible.style.width)-tailleBord;        
/*
    divTirette.DD=DragAndDrop;    
    divTirette.DD(
                {                
                objetADeplacer : divVisible
                ,grilleMagnetic:{heightRow:23}
                ,orientation    : 'v'              
                ,modCursor      : {avantDD : true, auClic : true}
               }
               );
*/
   rialto.widgetBehavior.affect(divTirette	
    							  ,'DragAndDrop'
    							  ,{
 								    oHtmlToMove		    	: divVisible
 								    ,bSelectMark 			: false
    							    ,isWithLimitsDisplayed  : false    							    
                          			,movingLimits 			: {orientation: 'v'}
									,magneticsGrid			: {heightRow : 23}
									}
    							  );
            
      
    divTirette.afterDD=function(dTop, dLeft){                        
    //--------------------------------------
        var decal=parseInt(dTop/22);                            
        oThis.decalFenVisible(decal);            
        oThis.liste.fermezoneMenu();             
    };
    

    divTirette.afterClic = function () {
    //--------------------------------
        oThis.liste.fermezoneMenu();
        oThis.activeTab(this.ind);
    };
    
    var ev = e || window.event;
    //open the menu
    this.liste.affichezoneMenu(ev,shift);     
}


//Update bnext and previous image according to the visible area
rialto.widget.TabFolder.prototype.majImages=function() {               
//========================       
     if(!this.autoRedimTab){
        //every tab is visible so hide image
        if(this.fenVisible.tailleFen==this.arrTabItem.length){            
    		//if not closable hide all the div
            if(!this.isClosable){
	            this.divImg.style.display='none';
	        }
	        else{
	        	//hide only the navigation button
		        this.previous.setVisible(false);        
	            this.next.setVisible(false);
	            this.MC.setVisible(false);
	            this.divImg.style.width=17;
	        }
        }
        else{
	        //size with or without close button
            if(!this.isClosable){	            
                this.divImg.style.width=60;
        	    this.divImg.style.display='block';
	        }
	        else{
		        this.divImg.style.width=70;
	        }     
            //enable button according to the navigation    
	        this.previous.setVisible(true);        
            this.next.setVisible(true);
            this.MC.setVisible(true);
            //if 1st visible is the first no previous
            if(this.fenVisible.indDeb==0){             
                this.previous.setEnable(false);  
            }
            else{
                this.previous.setEnable(true);
            }
            //if lastt visible is the last no next
            if(this.fenVisible.indFin==this.arrTabItem.length-1){             
                this.next.setEnable(false);
            }
            else{
                this.next.setEnable(true);
            }
        }
    }
}

/************************************************************************************************
 Objet TABITEM
 VERSION:0.85
 DATE:15/06/2006
 ************************************************************************************************
Init parameter
title title of the tab item
parentonglet: tabfolder where we put the tzb item in
enable: 	  initial enable state
*************************************************************************************************/

// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.TabItem=function(title,parentonglet,enable){
//========================       

    var objParam=new Object;
    //base object
    this.base = rialto.widget.AbstractContainer;
    objParam.type="tabitem";
    this.base(objParam);   
    this.enable=enable!=null?enable:true;
    this.autoResizeParent = parentonglet.autoResizeParent;   
    this.parentOnglet=parentonglet;
    var oThis=this;
    this.title=title;
    //TITLE DIV
    this.titreOnglet = document.createElement("DIV");
    this.titreOnglet.title=title;
    this.titreOnglet.id = "DIV_ONGLET";
    this.titreOnglet.style.width=this.parentOnglet.widthTabName+"px";
   	this.titreOnglet.className = "tabTitleDivOn";   
   	//left image
    this.imgG=document.createElement("DIV")
    this.imgG.className="leftEdgeTabOn";  
    //text  
    this.textDiv=document.createElement("DIV")
    this.textDiv.className="tabTitleTextOn";    
    this.textDiv.style.width=this.parentOnglet.widthTabName-5;
    this.textDiv.innerHTML=this.title;    
    //right image
    this.imgD=document.createElement("DIV")
    this.imgD.className="rightEdgeTabOn";    
    this.titreOnglet.appendChild(this.imgG);
    this.titreOnglet.appendChild(this.textDiv);
    this.titreOnglet.appendChild(this.imgD);
    //CONTENT DIV
    this.contenuOnglet = document.createElement("DIV");
    this.contenuOnglet.id = 'tabItem_'+rialto.widget.TabItem.prototype.nbInstances++;
    this.contenuOnglet.className = "ContenuOnglet";
    this.setVisible(false);
    this.contenuOnglet.oCiu=this;  
	//??
    this.contenuOnglet.onmousemove= function () {
    	return true;
    }
    this.setEnable(this.enable);
}

rialto.widget.TabItem.prototype = new rialto.widget.AbstractContainer;
rialto.widget.TabItem.prototype.nbInstances = 0;
/*
option / pb add Frame in inactive tabItem 
rialto.widget.TabItem.prototype.baseAdd =  rialto.widget.AbstractContainer.prototype.add;
rialto.widget.TabItem.prototype.add = function (elt) {
	var currentTabItem = this.parentOnglet.activeTab;
	var bActive =  (currentTabItem == this);
	if (!bActive) {	
		this.activeOnglet();
		traceExec('++++++ TabItem.activeOnglet();');
	}
	this.baseAdd(elt);
	if (!bActive)
		currentTabItem.activeOnglet();
}
*/



//TO ACTIVATE
rialto.widget.TabItem.prototype.setEnable=function(enable){
//========================      
	 if(enable){     	
        this.titreOnglet.style.fontStyle='';
    }
    else{               
        this.titreOnglet.style.fontStyle='italic';       
    }
    this.enable=enable;
}

//CHANGE THE VISIBLE STATE
rialto.widget.TabItem.prototype.setVisible=function(visible){
//=======================================================                
    if(visible){     	
        this.titreOnglet.style.display='block';
    	this.contenuOnglet.style.display='block';
    }
    else{
        this.titreOnglet.style.display='none';
    	this.contenuOnglet.style.display='none';	    
    }
    this.visible=visible;
}


//Display only the title
rialto.widget.TabItem.prototype.displayTitle=function(){
//========================       
    this.titreOnglet.style.display='block';    
    this.visible=true;
}



//change with by adding delta
rialto.widget.TabItem.prototype.changeWidth=function(delta){
//========================       
    if(delta==0){
        this.titreOnglet.style.width=0;    
    }
    else{
        this.titreOnglet.style.width=Math.max(0,parseInt(this.titreOnglet.style.width)+delta);    
    }
}

//set a new width
rialto.widget.TabItem.prototype.setWidth=function(width){    
//========================       
    this.titreOnglet.style.width=Math.max(0,width);
    this.textDiv.style.width=Math.max(0,width-5);    
}

//UPDATE SIZE
rialto.widget.TabItem.prototype.updateSize= function(){       

    this.contenuOnglet.style.overflow='hidden';
    if (this.autoResizableH){            
        this.updateHeight();        
    }   
    if(this.autoResizableW){        
        this.updateWidth();
    }    
    this.contenuOnglet.style.overflow= "auto";
    
}

rialto.widget.TabItem.prototype.updateWidth=function(){  	
    if(this.visible){
	    //get new width of the parent
	    var tailleCalc=parseInt(this.getNewParentWidth());    
	    this.contenuOnglet.style.width=tailleCalc;             
	    //call childs resize
	    this.resizeChilds(true,false);       
	}
}

rialto.widget.TabItem.prototype.updateHeight=function(){        
    if(this.visible){
	    //get new height of the parent
	    var tailleCalc=parseInt(this.getNewParentHeight());
	    this.contenuOnglet.style.height=tailleCalc;             
		//call childs resize
	    this.resizeChilds(false,true);       
    }
} 

rialto.widget.TabItem.prototype.getHtmlExt = function(){
//========================           
    return this.contenuOnglet;
}
rialto.widget.TabItem.prototype.getHtmlCont= function(){
//========================          
    return this.contenuOnglet;
}

rialto.widget.TabItem.prototype.onClose=function(){
//========================          
//The code must be redefine after the instanciation of the component. By default this method does nothing.
//raise when a the tab is close
}

rialto.widget.TabItem.prototype.activeOnglet=function(){            
//========================           
    if(this.enable){
    // hide the active tab
    if (this.parentOnglet.currentActiveTab!=this){
        if (this.parentOnglet.currentActiveTab){                           
            oldOng=this.parentOnglet.currentActiveTab;            
            oldOng.desactiveOnglet();                   
        }
        //new avtive tab
        this.parentOnglet.currentActiveTab=this;              
        this.imgG.className = 'leftEdgeTabOn';
        this.textDiv.className='tabTitleTextOn';
        this.imgD.className = 'rightEdgeTabOn';                 
        this.titreOnglet.className = 'tabTitleDivOn';               
        this.contenuOnglet.style.display='block';          
        // test / pb with scroll text in box in tab
        this.$$activeContent();                                              
        if(this.autoResizeParent){this.parentOnglet.updateSize();}    
    }
   }
}
rialto.widget.TabItem.prototype.desactiveOnglet=function(){            
//========================              
    this.contenuOnglet.style.display='none';                     
    this.imgG.className = 'leftEdgeTabOff';
    this.textDiv.className='tabTitleTextOff';
    this.imgD.className = 'rightEdgeTabOff';
    this.titreOnglet.className= 'tabTitleDivOff';   
    if (this.parentOnglet.currentActiveTab==this){this.parentOnglet.currentActiveTab=null;}
}

/*rialto.widget.TabItem.prototype.$$activeContent = function () {
	if (!this.arrChild) return;
//	traceExec('$$activeContent nb oRia= '+this.recORia.length,1);	
    for(var i=0; i < this.arrChild.length; i++) {  
    	child = this.arrChild[i];
//    	traceExec('i= '+i+ ' id= '+child.id,1);
 		if (this.arrChild[i].adaptToContext)
 			this.arrChild[i].adaptToContext();
     }	
}*/


rialto.widget.TabItem.prototype.release = function () {
    if (this.parentOnglet.currentActiveTab==this){this.parentOnglet.currentActiveTab=null;};
	this.titreOnglet.parentNode.removeChild(this.titreOnglet); 
	this.contenuOnglet.oCiu=null;  
    this.contenuOnglet.onmousemove=null;
    //D&D release    
    if(this.titreOnglet.DD){
    	this.titreOnglet.DD=null;
	    this.titreOnglet.afterDD = null;
		this.titreOnglet.afterClic = null;
		this.titreOnglet.onclick=null;
		DragAndDrop.release(this.titreOnglet);
	}
    
}

